<?php
namespace app\modules\content\models;

use app\modules\app\models\IeltsSpeakLike;
use yii\db\ActiveRecord;
use yii;

class IeltsSpeak extends ActiveRecord
{
    public static function tableName()
    {
        return '{{%ielts_speak}}';
    }

    public function rules()
    {
        return [
            // username and password are both required
            // [['pic', 'url'], 'required'],

        ];
    }

    //最近一季度
    public static function pastSpeak($year,$month,$part){
        $data = IeltsSpeak::find()
            ->where("((year=$year and month<$month) or (year<$year)) and part=$part")
            ->orderBy('year desc,month desc')
            ->asArray()
            ->one();
        $datas = [];
        if($data){
            $datas = IeltsSpeak::find()
                ->where("year={$data['year']} and month={$data['month']} and part=$part")
                ->orderBy('sort desc,id desc')
                ->asArray()
                ->all();
        }
        return $datas;
    }

    public static function getSpeakType($data){
        $tag = 2;
        if($data){
            $nowMonth = date('n');
            if ($nowMonth >= 1 && $nowMonth <= 4) {
                $month = 1; // 1-4月
            } elseif ($nowMonth >= 5 && $nowMonth <= 8) {
                $month = 2; // 5-8月
            } else {
                $month = 3; // 9-12月
            }
            $year = date('Y');
            if($month==$data[0]['month']&&$year==$data[0]['year']){
                $tag = 1;
            }
        }
        return $tag;
    }

    /**
     * 口语点赞
     * @param $uid
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public static function userLikeData($uid,$page=1,$pageSize=10){
        $offset = ($page-1)*$pageSize;
        $data = IeltsSpeakList::find()
            ->select('l.*,sl.part,sl.question,sl.pid')
            ->from('{{%ielts_speak_list}} sl')
            ->leftJoin('{{%ielts_speak_answer}} sa','sl.id=sa.pid')
            ->leftJoin('{{%ielts_speak_like}} l','l.contentId=sa.id')
            ->where("sa.uid=$uid and l.look=0 and l.type=1 and sl.id is not null")
            ->groupBy('sl.id')
            ->orderBy('l.id desc')
            ->offset($offset)
            ->limit($pageSize)
            ->asArray()
            ->all();
        foreach ($data as $k=>$v){
            $speak = IeltsSpeak::findOne($v['pid']);
            $data[$k]['name'] = '';
            if($speak){
                $data[$k]['name'] = $speak->cate_name;
            }
        }
        return $data;
    }

    /**
     * 口语点赞总数
     * @param $uid
     * @return int
     */
    public static function userLikeNum($uid){
        if(!$uid) return 0;
        $data = IeltsSpeakList::find()
            ->select('sl.id')
            ->from('{{%ielts_speak_list}} sl')
            ->leftJoin('{{%ielts_speak_answer}} sa','sl.id=sa.pid')
            ->leftJoin('{{%ielts_speak_like}} l','l.contentId=sa.id')
            ->where("sa.uid=$uid and l.look=0 and l.type=1 and sl.id is not null")
            ->groupBy('sl.id')
            ->asArray()
            ->all();
        $count = $data?count($data):0;
        return $count;
    }

    public static function changeStatus($uid,$id=0){
        if($id){
            $data = IeltsSpeakAnswer::find()
                ->select('l.id')
                ->from('{{%ielts_speak_answer}} sa')
                ->leftJoin('{{%ielts_speak_like}} l','l.contentId=sa.id')
                ->where("sa.uid=$uid and l.look=0 and l.type=1 and l.contentId=$id")
                ->asArray()
                ->all();
        }else{
            //一键已读
            $data = IeltsSpeakAnswer::find()
                ->select('l.id')
                ->from('{{%ielts_speak_answer}} sa')
                ->leftJoin('{{%ielts_speak_like}} l','l.contentId=sa.id')
                ->where("sa.uid=$uid and l.look=0 and l.type=1")
                ->asArray()
                ->all();
        }
        if($data){
            $ids = implode(',',array_column($data,'id'));
            IeltsSpeakLike::updateAll(['look'=>1],"id in ($ids)");
        }
    }
}